<!--Copyright (C) Kherty.  All rights reserved.-->
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:ss="http://schemas.Kherty.com/office/2007/spreadsheet" xmlns:ssui="clr-namespace:OpenLS.Spreadsheet.UI"
                    xmlns:drawing="http://schemas.Kherty.com/office/2007/drawing"
                    xmlns:Internals="clr-namespace:OpenLS.Spreadsheet.UI.Internals"
                    xmlns:Debugging="clr-namespace:OpenLS.Spreadsheet.Debugging" x:Class="OpenLS.Spreadsheet.UI.WorksheetResources">
  <ResourceDictionary.MergedDictionaries>
    <ssui:ContextMenuResources />
    <ResourceDictionary Source="/OpenLS.UI;component/Themes/ScrollBarResources.xaml" />
    <ssui:WorksheetResourcesHelpers />
    <ssui:WorksheetButtonResources />
    <ssui:WorksheetSplitterResources />
    <ssui:WorksheetPaneResources />
    <ssui:GroupResources></ssui:GroupResources>
  </ResourceDictionary.MergedDictionaries>

  <Style TargetType="{x:Type Grid}">
    <Setter Property="FocusVisualStyle" Value="{x:Null}" />
  </Style>

  <DataTemplate DataType="{x:Type ss:Worksheet}" x:Key="allTemplate">
        <AdornerDecorator Internals:FocusElement.FocusRedirect="{Binding ElementName=topLeftPane}"  Internals:FocusElement.FocusRoot="{Binding ElementName=topLeftPane}">
      <ssui:WorksheetPanel x:Name="worksheetGrid3" DataContext="{Binding}" SecondPaneRow="4" SecondPaneColumn="4"
                           >
                <Grid FocusVisualStyle="{x:Null}" Name="bottomRightCells" Margin="-1 -1 0 0 " 
              ssui:WorksheetPanel.DesiredWidth="{Binding BottomRightPane.DesiredWidth}"
              ssui:WorksheetPanel.DesiredHeight="{Binding BottomRightPane.DesiredHeight}" ssui:WorksheetPanel.Row="4"
              ssui:WorksheetPanel.Column="4" SnapsToDevicePixels="True" ClipToBounds="True">
                    <ContentPresenter Content="{Binding BottomRightPane}" ContentTemplate="{StaticResource paneCells}" Internals:FocusElement.FocusRedirect="{Binding RelativeSource={RelativeSource Self}}"  />
        </Grid>
        <ContentPresenter ssui:WorksheetPanel.IgnoreHeight="True" Name="bottomLeftRows" Internals:FocusElement.FocusRedirect="{Binding ElementName=bottomLeftCells}"
                          Content="{Binding BottomLeftPane}" Margin="-1 -1 0 0 " ContentTemplate="{StaticResource paneRows}"
                          ssui:WorksheetPanel.Row="4" ssui:WorksheetPanel.Column="1" />

        <Grid FocusVisualStyle="{x:Null}" Name="bottomLeftCells" Margin="-1 -1 0 0 "
              ssui:WorksheetPanel.DesiredWidth="{Binding BottomLeftPane.DesiredWidth}"
              ssui:WorksheetPanel.DesiredHeight="{Binding BottomLeftPane.DesiredHeight}" ssui:WorksheetPanel.Row="4"
              ssui:WorksheetPanel.Column="2" SnapsToDevicePixels="True" ClipToBounds="True" >
                    <ContentPresenter Content="{Binding BottomLeftPane}" ContentTemplate="{StaticResource paneCells}" Internals:FocusElement.FocusRedirect="{Binding RelativeSource={RelativeSource Self}}"  />
        </Grid>
        <ssui:WorksheetSplitter ssui:WorksheetPanel.Row="3" ssui:WorksheetPanel.ColumnSpan="5" ResizeDirection="Rows"
                                HorizontalAlignment="Stretch" VerticalAlignment="Center" x:Name="horizontalSplitter" />
        <ContentPresenter ssui:WorksheetPanel.IgnoreWidth="True" Name="topRightColumns" Internals:FocusElement.FocusRedirect="{Binding ElementName=topRightCells}"
                          Content="{Binding TopRightPane}" ContentTemplate="{StaticResource paneColumns}" Margin="-1 -1 0 0 "
                          ssui:WorksheetPanel.Column="4" ssui:WorksheetPanel.Row="1" />
        <Grid FocusVisualStyle="{x:Null}" Name="topRightCells" Margin="-1 -1 0 0 "
              ssui:WorksheetPanel.DesiredWidth="{Binding TopRightPane.DesiredWidth}"
              ssui:WorksheetPanel.DesiredHeight="{Binding TopRightPane.DesiredHeight}" ssui:WorksheetPanel.Row="2"
              ssui:WorksheetPanel.Column="4" SnapsToDevicePixels="True" ClipToBounds="True">
          <ContentPresenter Content="{Binding TopRightPane}" ContentTemplate="{StaticResource paneCells}" Internals:FocusElement.FocusRedirect="{Binding RelativeSource={RelativeSource Self}}" />
        </Grid>
        <ssui:WorksheetSplitter ssui:WorksheetPanel.Column="3" ssui:WorksheetPanel.RowSpan="5"
                                ResizeDirection="Columns" HorizontalAlignment="Center" VerticalAlignment="Stretch" x:Name="verticalSplitter" />

        <ssui:WorksheetSplitter ssui:WorksheetPanel.Row="3" ssui:WorksheetPanel.Column="3" ResizeDirection="Both"
                                HorizontalAlignment="Stretch" VerticalAlignment="Stretch" x:Name="centerSplitter"
                                RowsSplitter="{Binding ElementName=verticalSplitter}" ColumnsSplitter="{Binding ElementName=horizontalSplitter}" />
        <Button Margin="0 0 -1 -1" Name="selectAllButton"
                Visibility="{Binding Path=View.RowColumnHeadersVisible, Converter={StaticResource boolToVisibility}}"
                Template="{StaticResource worksheetButtonTemplate}" Command="{x:Static ssui:SpreadsheetEditingCommands.SelectAll}"
                ssui:WorksheetPanel.Row="1" ssui:WorksheetPanel.Column="1"></Button>

        <ContentPresenter ssui:WorksheetPanel.IgnoreHeight="True" Content="{Binding TopLeftPane}"
                          ContentTemplate="{StaticResource paneRows}" Margin="-1 -1 0 0 " Name="topLeftRows" ssui:WorksheetPanel.Row="2"
                          ssui:WorksheetPanel.Column="1" />
        <ContentPresenter ssui:WorksheetPanel.IgnoreWidth="True" Content="{Binding TopLeftPane}"
                          ContentTemplate="{StaticResource paneColumns}" Margin="-1 -1 0 0 " Name="topLeftColumns" ssui:WorksheetPanel.Column="2"
                          ssui:WorksheetPanel.Row="1" />

        <Grid FocusVisualStyle="{x:Null}" Name="topLeftCells"
              ssui:WorksheetPanel.DesiredWidth="{Binding TopLeftPane.DesiredWidth}"
              ssui:WorksheetPanel.DesiredHeight="{Binding TopLeftPane.DesiredHeight}" ssui:WorksheetPanel.Row="2"
              ssui:WorksheetPanel.Column="2" Margin="-1 -1 0 0 " SnapsToDevicePixels="True" ClipToBounds="True">
          <ContentPresenter Content="{Binding TopLeftPane}" ContentTemplate="{StaticResource paneCells}" 
                            ssui:WorksheetPanel.DesiredWidth="{Binding DesiredWidth}" ssui:WorksheetPanel.DesiredHeight="{Binding DesiredHeight}"
                            Name="topLeftPane" />
        </Grid>
        <Rectangle Fill="{DynamicResource {x:Static ss:SpreadsheetBrushes.GroupBackgroundKey}}"
                   HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
        </Rectangle>

        <ItemsControl ssui:WorksheetPanel.Column="1" Style="{StaticResource outlineButtons}"
                      Name="columnOutlineButtons">
          <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
              <StackPanel Orientation="Vertical"></StackPanel>
            </ItemsPanelTemplate>
          </ItemsControl.ItemsPanel>
          <ItemsControl.Tag>
            <Binding ConverterParameter="columns">
              <Binding.Converter>
                <Internals:RootGroupAccessor />
              </Binding.Converter>
            </Binding>
          </ItemsControl.Tag>
        </ItemsControl>
        <ItemsControl ItemsSource="{Binding ColumnGroups}"
                      Background="{DynamicResource {x:Static ss:SpreadsheetBrushes.GroupBackgroundKey}}"
                      ssui:WorksheetPanel.IgnoreWidth="True" VerticalAlignment="Stretch" Name="columnLeftGroups"
                      ssui:WorksheetPanel.Column="2" ItemContainerStyle="{StaticResource rowStyle}">
          <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
              <ssui:GroupPanel Orientation="Horizontal" VerticalAlignment="Stretch"
                               ContainerOffset="{Binding Path=TopLeftPane.LeftOffset}">
                <ssui:GroupPanel.Tag>
                  <MultiBinding ConverterParameter="columns">
                    <MultiBinding.Converter>
                      <ssui:DimensionOffsetObserverFactor />
                    </MultiBinding.Converter>
                    <Binding></Binding>
                    <Binding Path="TopLeftPane.Left" />
                  </MultiBinding>
                </ssui:GroupPanel.Tag>
              </ssui:GroupPanel>
            </ItemsPanelTemplate>
          </ItemsControl.ItemsPanel>
        </ItemsControl>

        <ItemsControl ItemsSource="{Binding ColumnGroups}"
                      Background="{DynamicResource {x:Static ss:SpreadsheetBrushes.GroupBackgroundKey}}" VerticalAlignment="Stretch"
                      Name="columnRightGroups" ssui:WorksheetPanel.Column="4" ItemContainerStyle="{StaticResource rowStyle}">
          <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
              <ssui:GroupPanel Orientation="Horizontal" VerticalAlignment="Stretch"
                               ContainerOffset="{Binding Path=TopRightPane.LeftOffset}">
                <ssui:GroupPanel.Tag>
                  <MultiBinding ConverterParameter="columns">
                    <MultiBinding.Converter>
                      <ssui:DimensionOffsetObserverFactor />
                    </MultiBinding.Converter>
                    <Binding></Binding>
                    <Binding Path="TopRightPane.Left" />
                  </MultiBinding>
                </ssui:GroupPanel.Tag>
              </ssui:GroupPanel>
            </ItemsPanelTemplate>
          </ItemsControl.ItemsPanel>
        </ItemsControl>

        <ItemsControl ssui:WorksheetPanel.Row="1" Style="{StaticResource outlineButtons}"
                      Name="rowOutlineButtons">
          <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
              <StackPanel Orientation="Horizontal"></StackPanel>
            </ItemsPanelTemplate>
          </ItemsControl.ItemsPanel>
          <ItemsControl.Tag>
            <Binding ConverterParameter="rows">
              <Binding.Converter>
                <Internals:RootGroupAccessor />
              </Binding.Converter>
            </Binding>
          </ItemsControl.Tag>
        </ItemsControl>

        <ItemsControl ItemsSource="{Binding RowGroups}"
                      Background="{DynamicResource {x:Static ss:SpreadsheetBrushes.GroupBackgroundKey}}"
                      ssui:WorksheetPanel.IgnoreHeight="True" VerticalAlignment="Stretch" Name="rowLeftGroups" ssui:WorksheetPanel.Row="2"
                      ItemContainerStyle="{StaticResource rowStyle}">
          <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
              <ssui:GroupPanel Orientation="Vertical" VerticalAlignment="Stretch"
                               ContainerOffset="{Binding Path=TopLeftPane.TopOffset}">
                <ssui:GroupPanel.Tag>
                  <MultiBinding>
                    <MultiBinding.Converter>
                      <ssui:DimensionOffsetObserverFactor />
                    </MultiBinding.Converter>
                    <Binding></Binding>
                    <Binding Path="TopLeftPane.Top" />
                  </MultiBinding>
                </ssui:GroupPanel.Tag>
              </ssui:GroupPanel>
            </ItemsPanelTemplate>
          </ItemsControl.ItemsPanel>
        </ItemsControl>
        <ItemsControl ItemsSource="{Binding RowGroups}"
                      Background="{DynamicResource {x:Static ss:SpreadsheetBrushes.GroupBackgroundKey}}" VerticalAlignment="Stretch"
                      Name="rowBottomGroups" ssui:WorksheetPanel.Row="4" ItemContainerStyle="{StaticResource rowStyle}">
          <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
              <ssui:GroupPanel Orientation="Vertical" VerticalAlignment="Stretch"
                               ContainerOffset="{Binding Path=BottomLeftPane.TopOffset}">
                <ssui:GroupPanel.Tag>
                  <MultiBinding>
                    <MultiBinding.Converter>
                      <ssui:DimensionOffsetObserverFactor />
                    </MultiBinding.Converter>
                    <Binding></Binding>
                    <Binding Path="BottomLeftPane.Top" />
                  </MultiBinding>
                </ssui:GroupPanel.Tag>
              </ssui:GroupPanel>
            </ItemsPanelTemplate>
          </ItemsControl.ItemsPanel>
        </ItemsControl>

        <!--   <Button Content="rowRightGroups" Width="40" Height="40" Name="rowRightGroups" ssui:WorksheetPanel.Row="4"/> -->
      </ssui:WorksheetPanel>
    </AdornerDecorator>
    <DataTemplate.Triggers>
      <DataTrigger Binding="{Binding View.RowColumnHeadersVisible}" Value="False">
        <Setter TargetName="columnOutlineButtons" Property="Width" Value="0" />
        <Setter TargetName="columnOutlineButtons" Property="Visibility" Value="Hidden" />
        <Setter TargetName="rowOutlineButtons" Property="Visibility" Value="Hidden" />
        <Setter TargetName="rowOutlineButtons" Property="Height" Value="0" />
      </DataTrigger>
      <DataTrigger Binding="{Binding TopRightPane}" Value="{x:Null}">
        <Setter TargetName="worksheetGrid3" Property="ssui:WorksheetPanel.SecondPaneColumn" Value="2" />
        <Setter TargetName="verticalSplitter" Property="Visibility" Value="Collapsed" />
        <Setter TargetName="centerSplitter" Property="Visibility" Value="Collapsed" />
        <Setter TargetName="columnRightGroups" Property="Visibility" Value="Collapsed" />
        <Setter TargetName="columnRightGroups" Property="ItemsSource" Value="{x:Null}" />

      </DataTrigger>
      <DataTrigger Binding="{Binding BottomLeftPane}" Value="{x:Null}">
        <Setter TargetName="worksheetGrid3" Property="ssui:WorksheetPanel.SecondPaneRow" Value="2" />
        <Setter TargetName="horizontalSplitter" Property="Visibility" Value="Collapsed" />
        <Setter TargetName="centerSplitter" Property="Visibility" Value="Collapsed" />
        <Setter TargetName="rowBottomGroups" Property="Visibility" Value="Collapsed" />
        <Setter TargetName="rowBottomGroups" Property="ItemsSource" Value="{x:Null}" />
      </DataTrigger>
    </DataTemplate.Triggers>
  </DataTemplate>

  <DataTemplate DataType="{x:Type ss:Worksheet}">
    <ContentControl Content="{Binding}" Name="worksheetControl" ContentTemplate="{StaticResource allTemplate}" Internals:FocusElement.FocusDown="True"
                    drawing:DrawingElement.Theme="{Binding Workbook.Theme}"
                    drawing:DrawingElement.ColorScheme="{Binding Workbook.Theme.ColorScheme}" ssui:SpreadsheetElement.Worksheet="{Binding}"
                    ssui:SpreadsheetElement.WorksheetViewModel="{Binding Converter={x:Static Internals:SpreadsheetConverters.ViewModelFactory}}"
                    TextElement.FontFamily="{Binding Path=Workbook.FontFamily,Mode=OneWay}">
      <Internals:EditionElement.EditorRoot>
        <MultiBinding Converter="{x:Static Internals:SpreadsheetConverters.FirstNonNullConverter}">
          <Binding RelativeSource="{RelativeSource Self}" Path="(Internals:EditionElement.EditorRoot)"></Binding>
          <Binding ElementName="worksheetControl"></Binding>
        </MultiBinding>
      </Internals:EditionElement.EditorRoot>
      <ContentControl.LayoutTransform>
        <ScaleTransform x:Name="scaleTransform"
                        ScaleX="{Binding Path=View.ZoomScale, Converter={x:Static Internals:SpreadsheetConverters.FromPercentConverter}}"
                        ScaleY="{Binding Path=View.ZoomScale, Converter={x:Static Internals:SpreadsheetConverters.FromPercentConverter}}" />

      </ContentControl.LayoutTransform>

      <ContentControl.CommandBindings>
        <CommandBinding Command="{x:Static ssui:SpreadsheetEditingCommands.SelectAll}" Executed="OnSelectAll" />
        <CommandBinding Command="{x:Static drawing:ThemeCommands.SetColorScheme}" CanExecute="OnTrue"
                        Executed="OnSetColorScheme" />
        <CommandBinding Command="{x:Static drawing:ThemeCommands.SetTheme}" CanExecute="OnTrue" Executed="OnSetTheme" />
        <CommandBinding Command="{x:Static drawing:ThemeCommands.SetFontScheme}" CanExecute="OnTrue"
                        Executed="OnSetFontScheme" />
      </ContentControl.CommandBindings>
    </ContentControl>
  </DataTemplate>

</ResourceDictionary>